﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class newStudentRegisterClass : Form
    {

        public newStudentRegisterClass()
        {
            InitializeComponent();
        }


        private void newStudentRegisterClass_Load(object sender, EventArgs e)
        {
            loadComboGender();
            CourseDataProvider course = new CourseDataProvider();
            DataSet dscourse = course.loadCourses();
            cbbCourse.DataSource = dscourse.Tables[0];
            cbbCourse.DisplayMember = "NameCourse";
            cbbCourse.ValueMember = "ID";
        }


        private void button1_Click(object sender, EventArgs e)
        {           
            Close();
        }

        public void loadComboGender()
        {
            GenderDataProvider gender = new GenderDataProvider();
            DataSet ds = gender.loadDs();
            comboBox_Gender.DataSource = ds.Tables[0];
            comboBox_Gender.DisplayMember = "Gender";
            comboBox_Gender.ValueMember = "IDGender";
        }
        
     

        private void button2_Click(object sender, EventArgs e)
        {
            string name = txtFullName.Text.ToString();
            string birthday = txtBirthday.Text.ToString();
            string job = txtJob.Text.ToString();
            string email = txtEmail.Text.ToString();
            int phone = int.Parse(txtPhone.Text.ToString());
            string address = txtAddress.Text.ToString();

            int idgender = int.Parse(comboBox_Gender.SelectedValue.ToString());

            DataStudentProvider std = new DataStudentProvider();
            int result1 = std.insertStudent(name, idgender, birthday, job, phone, email, address);

            StudentInClassDataProvider stdClass = new StudentInClassDataProvider();
            int result2 = stdClass.InsertStudentInClass(result1, int.Parse(cbbClass.SelectedValue.ToString()));

            if (result2 != 0)
                MessageBox.Show("Đăng ký thành công");
            else MessageBox.Show("Đăng ký không thành công");
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            txtAddress.Clear();
            txtBirthday.Clear();
            txtEmail.Clear();
            txtFullName.Clear();
            txtJob.Clear();
            txtPhone.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void cbbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbCourse.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                DataSet dsclass = cls.loadClass(cbbCourse.SelectedValue.ToString());
                cbbClass.DataSource = dsclass.Tables[0];
                cbbClass.DisplayMember = "NameClass";
                cbbClass.ValueMember = "ID";
            }
        }

        private void cbbClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbClass.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                SqlDataReader sdr = cls.ClassDetail(cbbClass.SelectedValue.ToString());
                if (sdr.Read())
                {
                    txtFee.Text = sdr.GetValue(0).ToString();
                    txtStartDate.Text = sdr.GetValue(1).ToString();
                    txtEndDate.Text = sdr.GetValue(2).ToString();
                }
                sdr.Close();
            }
        }

    }
}
